package com.bl.order.service.impl;

import com.bl.basic.util.PageList;
import com.bl.order.domain.OrderPetAcquisition;
import com.bl.order.dto.OrderDto;
import com.bl.order.mapper.OrderPetAcquisitionMapper;
import com.bl.order.query.OrderPetAcquisitionQuery;
import com.bl.order.service.IOrderPetAcquisitionService;
import com.bl.basic.service.impl.BaseServiceImpl;
import com.bl.org.domain.Employee;
import com.bl.org.domain.Shop;
import com.bl.org.mapper.EmployeeMapper;
import com.bl.org.mapper.ShopMapper;
import com.bl.pet.domain.Pet;
import com.bl.pet.domain.SearchMasterMsg;
import com.bl.pet.mapper.PetMapper;
import com.bl.pet.mapper.SearchMasterMsgMapper;
import org.apache.tomcat.websocket.PerMessageDeflate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;


    /**
     *  订单确认
     * @param dto
     */
    @Override
    public void confirm(OrderDto dto) {
        Long ordetId = dto.getId();
        BigDecimal money = dto.getMoney();
        Integer payType = dto.getPayType();
        /**
         * 1. 改订单状态，金额, 支付方式
         * 2. 改寻主消息状态
         * 3. 改pet金额
         */
        // 1. 改订单状态，金额, 支付方式
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(ordetId);
        orderPetAcquisition.setState(2);
        orderPetAcquisition.setPrice(money);
        orderPetAcquisition.setPaytype(payType);
        orderPetAcquisitionMapper.update(orderPetAcquisition);

        //  2. 改寻主消息状态
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(orderPetAcquisition.getSearchMasterMsgId());
        searchMasterMsg.setState(2);
        searchMasterMsgMapper.update(searchMasterMsg);
        //  3. 改pet金额
        Pet pet = petMapper.loadById(orderPetAcquisition.getPetId());
        pet.setCostprice(money);
        petMapper.update(pet);
    }

    /**
     *  分页显示当前登陆人的店铺订单
     * @param query
     * @param id
     * @return
     */
    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, Long id) {
        Employee employee = employeeMapper.loadByLogininfoId(id);
        if (employee.getShopId() != null){
            // 是店铺人员
            Shop shop = shopMapper.loadById(employee.getShopId());
            if (shop.getAdminId() != employee.getId()){// 是店员不是店长
                // 设置 empid为 当前店铺店长id给query
                query.setEmpId(employee.getId());
            }
            // 设置query的shopId
            query.setShopId(employee.getShopId());
        }
        return super.queryPage(query);
    }

    @Override
    public void cancel(Long id) {
         //1. 修改订单状态
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
         //2. 寻主消息的shopid=null
        searchMasterMsgMapper.reject(orderPetAcquisition.getSearchMasterMsgId());
         // 3. pet 删除
        petMapper.remove(orderPetAcquisition.getPetId());




    }
}
